package org.dialMsg.Server.ServerImp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dialMsg.Topic.UrlConstants;
import org.dialMsg.Uitils.CryptUtil;

import java.util.ArrayList;

/**
 * @author DPF
 */

public class MyCryValue {
    private static final String Salt = "rvM2iePUN@cvB56AbtSEP2Zj4e4zGsJ";
    private static final String Pwd = "123456zZ@";

    public static String getMyCryToken(String name) {
        String accessToken = null;
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            String filledUrl = getFilledUrl(name);
            HttpPost httpPost = new HttpPost(filledUrl);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    String responseString = EntityUtils.toString(response.getEntity());
                    JSONObject jsonObject = JSON.parseObject(responseString);
                     accessToken = (String) jsonObject.get("access_token");
                } else {
                    return "暂无token数据";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    public static ArrayList<String[]> getMyCryTokenList() {
        ArrayList<String> namelist = new ArrayList<>();
        for (int i = 1; i <= 1010; i++) {
            String name = "hnzhzx0" + i;
            namelist.add(name);
        }

        ArrayList<String[]> tokenList = new ArrayList<>();
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
//            System.out.println("开始发送请求");
            for (String name : namelist) {
                String filledUrl = getFilledUrl(name);
                HttpPost httpPost = new HttpPost(filledUrl);
                httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

                try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                    int statusCode = response.getStatusLine().getStatusCode();
                    if (statusCode == 200) {
                        String responseString = EntityUtils.toString(response.getEntity());
                        JSONObject jsonObject = JSON.parseObject(responseString);
                        String accessToken = (String) jsonObject.get("access_token");
                        tokenList.add(new String[]{name, accessToken}); // 将 name 和 accessToken 组合成一个数组
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tokenList;
    }

    private static String getFilledUrl(String name) {
        String cryptedSm3Pwd = CryptUtil.cryptSm3(Salt, Pwd);
        String cryptedSha1Pwd = CryptUtil.cryptSha1(Salt,Pwd);
        String urlTemplate = UrlConstants.BASE_URL+"/api/oauth/token/getAccessToken?" +
                "client_id=ROBOT&client_secret=1" +
                "&grant_type=password&remember=1&username=%s" +
                "&password=%s";
        String filledUrl = String.format(urlTemplate, name,cryptedSm3Pwd);
        return filledUrl;
    }
}
